package mx.netbrain.business;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

import org.apache.log4j.Logger;

public class DataBaseBusiness {

	private static final Logger logger = Logger
			.getLogger(DataBaseBusiness.class);

	public static boolean test(String driver, String host, String username,
			String password) {
		logger.info("JDBC Tester v1.0");
		try {
			Statement stmt;
			// Register the JDBC driver for MySQL.
			Class.forName(driver);
			// Define URL of database server for
			// database named mysql on the localhost
			// with the default port number 3306.
			String url = host + "mysql";
			// Get a connection to the database for a
			// user named xxx with a xxx password.
			Connection con = DriverManager.getConnection(url, username,
					password);

			// Display URL and connection information
			logger.info("URL: " + url);
			logger.info("Connection: " + con);

			// Get a Statement object
			stmt = con.createStatement();

			// Execute a simple command;We do not trap the response
			// We are only wanting a connection test
			boolean result;
			result = stmt.execute("show tables;");
			if (result) {
				logger.info("SQL command was executed sucessfully");
				return true;
			}
			con.close();
			stmt.close();
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
		}// end catch
		return false;
	}// end main

}
